define([], function() {
    'use strict';

    /**
     * @param id - switch容器id
     * @param openText - 开启的文字，默认启用
     * @param closeText - 关闭的文字，默认停用
     * @param value - 显示的状态，true-开启，false-关闭，默认为false
     * @param disabled - 是否禁用状态，true-禁用，false-非禁用，默认为false
     * 
     * @function getValue - 实例获取当前switch的状态true/false
     * @function setValue - 设置当前实例的状态
     * @function setDisabled - 设置当前实例的禁用状态
     * 
     * @callback onChange - 改变状态的时候回调
     */
    function Switch(option){
        if(!option.id){
            throw new Error('缺少必填项，容器id');
        }
        this.boxId = option.id;
        this.box = document.getElementById(this.boxId);
        this.box.className = 'yby-switch-box';
        this.openText = option.openText || "启用";
        this.closeText = option.closeText || "停用";
        this.value = !option.value ? false : true;
        this.disabled = !option.disabled ? false : true;
        this.onChange = option.onChange;
        this.init();
    }

    Switch.prototype.init = function(){
        this.render();
        this.setListenFunc();
        this.setListener();
        this.setValue(this.value);
        this.setDisabled(this.disabled);
    }

    Switch.prototype.render = function(){
        var str = '<div class="switch-box">'
            + '<div class="bg-box"></div>'
            + '<div class="show-box open">'
            + '<span class="show-msg">启用</span>'
            + '<span class="circle"></span>'
            + '</div>'
            + '</div>';
        this.box.innerHTML = str;
    }

    Switch.prototype.setListenFunc = function(){
        var that = this;
        this.click = function(e){
            var _this = e.target;
            var box = $(_this);
            if(!box.hasClass("switch-box")){
                box = $(_this).parents(".switch-box").eq(0);
            }
            if(box.hasClass("disabled")){
                return;
            }else{
                // that.setValue(!that.getValue());
            }
            if(that.onChange){
                that.onChange(that.getValue());
            }
        }
    }

    Switch.prototype.destroy = function(){
        $(document).off('click', '#' + this.boxId + ' .switch-box', this.click);
    }

    Switch.prototype.setListener = function(){
        $(document).off('click', '#' + this.boxId + ' .switch-box', this.click);
        $(document).on('click', '#' + this.boxId + ' .switch-box', this.click);
    }
    
    Switch.prototype.getValue = function(){
        var switchBox = $("#" + this.boxId).find(".switch-box");
        return switchBox.hasClass("open") ? true : false;
    }

    Switch.prototype.setValue = function(value){
        var switchBox = $("#" + this.boxId).find(".switch-box");
        switchBox.removeClass("open close");
        switchBox.addClass(value ? "open" : "close");
        switchBox.find(".show-msg").text(value ? this.openText : this.closeText);
    }

    Switch.prototype.setDisabled = function(disabled){
        var switchBox = $("#" + this.boxId).find(".switch-box");
        switchBox.removeClass("disabled");
        if(disabled){
            switchBox.addClass("disabled");
        }
    }

    return Switch;
});